<template>
  <div class="mt-1">
    <van-form>
      <van-field
        label="盘存单号"
        placeholder="点击此处获取关联单号"
        :value="orderNum"
        :label-width="labelWidth"
        is-link
        readonly
        @click="getOrderNum"
      ></van-field>
      <van-field
        v-model="code"
        :label-width="labelWidth"
        clearable
        label="拍码"
        ref="code"
        @focus="focus"
        placeholder="请输入拍码"
      />
      <ag-grid-vue
        style="width: 100%; height: 50vh"
        class="ag-theme-alpine mt-1"
        :columnDefs="columnDefs"
        :defaultColDef="defaultColDef"
        :gridOptions="gridOptions"
        rowSelection="single"
        :rowData="rowData"
      >
      </ag-grid-vue>
      <van-row type="flex" gutter="30" justify="center">
        <van-col span="7">
          <van-button type="danger" class="mt-1" @click="onRemoveSelected" block
            >删除</van-button
          ></van-col
        >
        <van-col span="7"
          ><van-button type="info" class="mt-1" @click="onSubmit" block
            >提交</van-button
          ></van-col
        >
      </van-row>
      <van-popup
        v-model="ShowStatue"
        position="bottom"
        style="height: 100%"
        class="bg-default"
      >
        <show-send-data :ShowDataB="ShowData" @get-val="Sure" />
      </van-popup>
      <van-popup
        v-model="OrderStockVisible"
        position="bottom"
        style="height: 100%"
        class="bg-default"
      >
        <OrderStock @close="close" />
      </van-popup>
    </van-form>
  </div>
</template>

<script>
import { AgGridVue } from "ag-grid-vue";
import { parseQRCode, toast } from "../../untils/commion";
import { InformationSubmit, getDetail } from "../../assets/api/post";
import ShowSendData from "../../components/ShowSendDate.vue";
import { AgGrid } from "../../minx/AgGrid";
import OrderStock from "./OrderStock.vue";

export default {
  name: "Warehousing",
  components: { AgGridVue, ShowSendData, OrderStock },
  mixins: [AgGrid],
  data() {
    return {
      labelWidth: "7em",
      titles: new Map().set(9, "仓库盘存"),
      type: null,
      deliveryPlant: "",
      fieldValue: "",
      ShowStatue: false,
      OrderStockVisible: false,
      deliveryPlantVisible: false,
      code: "",
      orderNum: "",
      StockCheckId: "",
      columnDefs: [
        { headerName: "物料", field: "productid", flex: 1.5 },
        { headerName: "拍号", field: "uniqueid", flex: 2 },
        { headerName: "数量", field: "stockquantity", flex: 1.5 },
      ],
      rowData: [],
      detailData: [],
      ShowData: {}, //展示的数据
    };
  },
  watch: {
    code(newValue) {
      if (this.orderNum) {
        if (newValue.length > 9) {
          let paresqrdata = parseQRCode(newValue);
          if (paresqrdata) {
            if (
              !this.rowData.find((it) => it.uniqueid == paresqrdata.palletNo)
            ) {
              //获取数据
              this.getDetailInfo(paresqrdata.palletNo);
              paresqrdata = "";
              this.code = "";
            } else {
              toast("拍码重复");
              this.code = "";
            }
          } else {
            toast("未识别成功");
            this.code = "";
          }
        }
      } else {
        toast("请先选择关联单号");
      }
      this.code = "";
    },
  },
  methods: {
    // 获取焦点时触发的方法
    focus() {
      // 获取焦点时阻止键盘弹出
      this.$refs.code.$refs.input.setAttribute("readonly", "readonly");
      setTimeout(() => {
        this.$refs.code.$refs.input.removeAttribute("readonly");
      }, 200);
    },
    close() {
      this.orderNum = this.$store.state.BillNO;
      this.StockCheckId = this.$store.state.StockCheckId;
      this.OrderStockVisible = false;
    },
    setTitle() {
      let res = this.titles.get(this.type);
      this.$store.commit("SET_NAVIGATION", res);
    },
    onFinish({ selectedOptions }) {
      this.deliveryPlantVisible = false;
      this.deliveryPlantAndWarehouse = selectedOptions.map((item) => item.id);
      this.fieldValue = selectedOptions.map((option) => option.name).join("/");
    },
    onRemoveSelected() {
      let selectedData = this.gridApi.getSelectedRows();
      if (this.rowData[0] == null) {
        this.$toast("当前表格为空");
      } else {
        if (this.selectedData[0] == null) {
          this.$toast("请选择要删除的数据");
        } else {
          // let a = this.gridApi.applyTransaction({ remove: selectedData });
          // let selectIndex = this.rowData.indexOf(a.remove[0]);
          // this.rowData.splice(selectIndex - 1, 1);
          // this.detailData.splice(selectIndex - 1, 1);

          let selctIndex = this.gridApi.applyTransaction({
            remove: selectedData,
          });
          // 删除选中的数据
          this.rowData.splice(selctIndex.remove[0].childIndex, 1);
          this.detailData.splice(selctIndex.remove[0].childIndex, 1);
        }
      }
    },
    getOrderNum() {
      this.OrderStockVisible = true;
    },
    //获取后台信息
    async getDetailInfo(val) {
      let res = await getDetail(val);
      // console.log(res);
      if (res.status == 200) {
        this.ShowStatue = true;
        this.ShowData = JSON.parse(JSON.stringify(res.data[0]));
        // console.log(this.ShowData);
        if (this.ShowData) {
          this.rowData.push(res.data[0]);
        }
        // console.log(res);
      } else {
        toast(res.msg);
      }
      this.code = "";
    },
    //传回来把组件关闭
    Sure() {
      this.ShowStatue = false;
      this.getOther();
    },
    //主列表数据
    getOther() {
      this.detailData.push({
        PalletNo: this.ShowData.uniqueid,
        ActSumBoxSure: this.ShowData.stockquantity,
      });
    },
    async onSubmit() {
      if (this.rowData.length == 0) {
        this.$toast("当前表格为空");
      } else {
        let type = "StockCheck";
        let submit = {
          BillNo: this.orderNum,
          StockCheckId: this.StockCheckId,
          ModifiedId: "",
          ModifiedName: "",
        };
        let res = await InformationSubmit(
          type,
          JSON.stringify(submit),
          JSON.stringify(this.detailData)
        );
        if (res.d.state == "success") {
          this.rowData = [];
          this.detailData = [];
          if (this.rowData.length == 0) {
            toast(res.d.message);
          }
        } else {
          toast(res.d.message);
        }
        // console.log(res);
      }
    },
    // 关闭enter键
    closeEnter() {
      document.onkeydown = function (e) {
        //捕捉回车事件
        var ev = typeof event != "undefined" ? window.event : e;
        if (ev.keyCode == 13 || event.which == 13) {
          return false;
        }
      };
    },
  },
  mounted() {
    this.$route.meta.keepAlive = true;
    this.type = parseInt(this.$route.params.type);
    this.setTitle();
    this.closeEnter();
  },
};
</script>

<style lang="less" scoped>
.ag-theme-alpine .ag-root-wrapper {
  border-left-width: 0;
  border-right-width: 0;
}
</style>

